#include "thumbnailjob.h"

#include <jobs/kdcstandardkprocessjob.h>
#include <jobs/kdcmplayermencoderjob.h>
#include <KStandardDirs>

ThumbnailJob::ThumbnailJob( const QString &aMovieFile, int aPos, bool aScaled, QObject *aParent ) : KDCFrameJobInterface( aParent )
{
	KDCMPlayerMencoderJob *mJob = new KDCMPlayerMencoderJob( KDCMPlayerMencoderJob::MPlayer, this );
	mJob->addArgument( "-vo", "jpeg:outdir=" + pDir.name() );
	mJob->addArgument( "-ss", QString::number( aPos ) );
	mJob->addArgument( "-endpos", QString::number( aPos ) );
	mJob->addArgument( "-nosound" );
	mJob->addArgument( "-noautosub" );
	mJob->addArgument( "-frames", "1" );
	mJob->setFile( aMovieFile );
	mJob->setTotalSize( 1 );
	addSubjob( mJob );

	if ( aScaled )
	{
		KDCStandardKProcessJob *sJob = new KDCStandardKProcessJob( this );
		sJob->setProgram( "convert" );
		sJob->setWorkingDirectory( pDir.name() );
		sJob->addArgument( "-resize", QString( "184x184" ) );
		sJob->addArgument( "00000001.jpg" );
		sJob->addArgument( "-thumbnail", "184x106" );
		sJob->addArgument( "-bordercolor", "black" );
		sJob->addArgument( "-border", "80" );
		sJob->addArgument( "-gravity", "center" );
		sJob->addArgument( "-crop", "184x106+0+0" );
		sJob->addArgument( "+repage" );
		sJob->setOutputFile( "out.png" );
		sJob->workAsShell( true );
		sJob->setTotalSize( 1 );
		addSubjob( sJob );
	}
	else
	{
		KDCStandardKProcessJob *sJob = new KDCStandardKProcessJob( this );
		sJob->setProgram( "convert" );
		sJob->setWorkingDirectory( pDir.name() );
		sJob->addArgument( "-resize", QString( "184x184" ) );
		sJob->addArgument( "00000001.jpg" );
		sJob->setOutputFile( "out.png" );
		sJob->workAsShell( true );
		sJob->setTotalSize( 1 );
		addSubjob( sJob );
	}  
}

ThumbnailJob::~ThumbnailJob()
{

}

QPixmap ThumbnailJob::resultFrame() const
{
	return QPixmap( pDir.name() + "/out.png" );
}
